package com.zx.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.reggie.common.CustomException;
import com.zx.reggie.common.R;
import com.zx.reggie.mapper.CategoryMapper;
import com.zx.reggie.mapper.DishMapper;
import com.zx.reggie.mapper.SetmealMapper;
import com.zx.reggie.pojo.Category;
import com.zx.reggie.pojo.Dish;
import com.zx.reggie.pojo.Setmeal;
import com.zx.reggie.service.CategoryService;
import com.zx.reggie.service.DishService;
import com.zx.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 张旭
 * @version 1.0
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {

        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count = dishService.count(lambdaQueryWrapper);

        if(count > 0){
            throw new CustomException("删除菜系失败，该菜系与菜品关联");
        }

        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(lambdaQueryWrapper1);

        if(count1 > 0) {
            throw new CustomException("删除套餐失败，该套餐与菜品关联");
        }

        super.removeById(id);
        return;
    }
}
